﻿/**********************  此代码是由代码生成器生成，生成时间： 2012/12/4 21:29:00  ************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CRM.Entity.Extra;
using CRM.Entity;
using CRM.DAL;
using CRM.Entity.Page;
using CRM.Index.IProvider;
using System.Configuration;

namespace CRM.Controller
{
    public partial class UserInfoController
    {
        static readonly string address = ConfigurationManager.AppSettings["address"];

        UserInfoDAL  userInfoDAL = new  UserInfoDAL();

        ChannelFactory<IProviderService> channelFactory;

        public UserInfoController()
        {
             channelFactory = new ChannelFactory<IProviderService>(new NetTcpBinding(), new EndpointAddress(new Uri(address)));
        }

        public UserInfoPage GetList(PageInfo page)
        {
            UserInfoPage list = new UserInfoPage();

            try
            {
                list = channelFactory.CreateChannel().UserInfo_Page(page);
            }
            catch (Exception ex)
            {
                throw;
            }
            return list;
        }
        
        /*********************************** UserInfoExtra **************************************/
        public UserInfoExtraPage GetListExtra(PageInfo page)
        {
            UserInfoExtraPage list = new UserInfoExtraPage();

            try
            {
              list = channelFactory.CreateChannel().UserInfoExtra_Page(page);
            }
            catch (Exception ex)
            {
                throw;
            }
            return list;
        }

        public UserInfoPage GetListTop(int top)
        {
            UserInfoPage list = new UserInfoPage();

            try
            {
                list = channelFactory.CreateChannel().UserInfo_Top(top);
            }
            catch (Exception ex)
            {
                throw;
            }

            return list;
        }

        public UserInfo GetSingle(Int32 id)
        { 
          try
           {
                var single = channelFactory.CreateChannel().UserInfo_Single(id);

                return single;
           }
          catch (Exception ex)
          {
             throw;
          }
        }

        public UserInfo GetSingle(string userName)
        {
            try
            {
               var single =  channelFactory.CreateChannel().UserInfo_SingleByName(userName);
               return single;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public bool Add(UserInfo userInfo)
        {
            bool isSuccess = false;

            try
            {
                userInfo.CreateTime = DateTime.Now;
                userInfo.LastLoginTime = DateTime.Now;
                userInfo.Disable = false;
                isSuccess = userInfoDAL.UserInfo_Add(userInfo);
            
                if(!isSuccess)
                 return false;
               
                isSuccess=channelFactory.CreateChannel().UserInfo_Insert(userInfo);

            }
            catch (Exception ex)
            {
                throw;
            }

            return isSuccess;
        }

        public bool Update(UserInfo userInfo)
        {
            bool isSuccess = false;

            try
            {               
                isSuccess = userInfoDAL.UserInfo_Update(userInfo);
            
               if(!isSuccess)
                return false;

                isSuccess= channelFactory.CreateChannel().UserInfo_Update(userInfo);
            }
            catch (Exception ex)
            {
               throw;
            }

            return isSuccess;
        }

        public bool Delete(UserInfo userInfo)
        {
            bool isSuccess = false;

            try
            {
                isSuccess = userInfoDAL.UserInfo_Remove(userInfo);
            
               if(!isSuccess)
                return false;

               isSuccess= channelFactory.CreateChannel().UserInfo_Delete(userInfo);

            }
            catch (Exception ex)
            {
              throw;
            }

            return isSuccess;
        }
    }
}
